﻿using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NuPattern.VisualStudio.Solution;

namespace MVCAppToolkit.UserTests
{
    public class CreateMvcApplicationSpec
    {
        [TestClass]
        public class GivenAddNewDialog : CreateToolkitTest
        {
            [TestInitialize]
            public override void InitializeContext()
            {
                base.InitializeContext();

                // TODO: In the VS2010 version, weed a way to press the OK button in wizard that pops up
                base.CreatePatternFromProjectTemplate(MvcAppToolkitInfo.VsTemplateMvcAppToolkit);
            }

            [TestCleanup]
            public override void Cleanup()
            {
                base.Cleanup();
            }

            [HostType("VS IDE")]
            [TestMethod, TestCategory("Integration")]
            public void ThenProjectTemplateUnfolded()
            {
                // Project is unfolded into solution
                var project = solution.Find<IProject>(p => p.Name == this.projectName).First();
                Assert.IsNotNull(project);

                // Pattern exists 
                var pattern = this.patternManager.Products.FirstOrDefault(p => p.InstanceName == this.projectName);
                Assert.IsNotNull(pattern);

                // Pattern linked to project
                Assert.IsTrue(pattern.References.Any(r => r.Value == string.Format("solution://{0}/", project.Id)));

                // Guidance Workflow exists
                var registrations = this.guidanceManager.InstalledGuidanceExtensions;
                var feature = registrations.FirstOrDefault(registration => registration.ExtensionId == MvcAppToolkitInfo.VsixIdentifier);
                Assert.IsNotNull(feature);
            }
        }

        [TestClass]
        public class GivenSolutionBuilder : CreateToolkitTest
        {
            [TestInitialize]
            public override void InitializeContext()
            {
                base.InitializeContext();

                // TODO: In the VS2010 version, weed a way to press the OK button in wizard that pops up
                base.CreatePatternFromPatternManager(MvcAppToolkitInfo.VsixIdentifier);
            }

            [TestCleanup]
            public override void Cleanup()
            {
                base.Cleanup();
            }

            [HostType("VS IDE")]
            [TestMethod, TestCategory("Integration")]
            public void ThenPatternElementCreated()
            {
                // Project is unfolded into solution
                var project = solution.Find<IProject>(p => p.Name == this.projectName).First();
                Assert.IsNotNull(project);

                // Pattern exists 
                var pattern = this.patternManager.Products.FirstOrDefault(p => p.InstanceName == this.projectName);
                Assert.IsNotNull(pattern);

                // Pattern linked to project
                Assert.IsTrue(pattern.References.Any(r => r.Value == string.Format("solution://{0}/", project.Id)));

                // Guidance Workflow exists
                var registrations = this.guidanceManager.InstalledGuidanceExtensions;
                var feature = registrations.FirstOrDefault(registration => registration.ExtensionId == MvcAppToolkitInfo.VsixIdentifier);
                Assert.IsNotNull(feature);
            }
        }
    }
}
